/*
 * AALItem.h
 *
 *  Created on: 17.05.2011
 *      Author: sili
 */

#ifndef AALITEM_H_
#define AALITEM_H_

#include <vdr/osd.h>
#include <vdr/tools.h>
#include <vdr/thread.h>
#include <vdr/plugin.h>

#include "Controller.h"
#include "TemplateFactory.h"

#include "Visualizer.h"

using namespace Magick;

class AALItem{
protected:
	Controller * controller;

	timeval start, end;
	cMutex aalItemMutex;
	bool active;
	int visibility;

	bool fixedHotkey;					// if true, the hotkey comes from system.xml (it can't be changed)
	int hotkey;							// dynamic hotkey. They may exist max 99 hotkeys

	int id;								// Each item has an unique id because an another item may have a parent which references to this id
	string name;						// the name e.g. Wohnzimmer, Lichtschalter, Heizung
	string type;						// the type e.g. flat, room, sensor, actuator
	string templateName;				//
	AALItem * parent;					// the parent
	int globalId;						// self increasing id (used to be able to debug which instance was constructed and which was destructed)
	vector<AALItem *> children;
public:
	Image image;						// size of the image is dependent of this->relativePositions

	int depth;

	// rename me to e.g. relativeRatio!!!!
	AALArea relativePositions;			// relativePositions are only managed by fromSVG (which calculates the positions of each sublement)

	// Areas which are currently used for the visualization. This values may be overwritten at any p
	AALArea area;

	AALItem(int _id, Controller * _controller);
	virtual ~AALItem();
	void setId(int _id);
	int getId();

	void setActive(bool _active);
	bool isActive();

	bool hasFixedHotkey();
	void setFixedHotkey(bool fixed);

	int getHotkey();
	virtual void setHotkey(int _hotkey);
	void eraseHotkeys(bool _findRootFirst = false);
	void getFreeHotkeys(bool _findRootFirst, set<int> & _freeHotkeys);
	void createHotkeys();				// create hotkeys for my first depth children
	int getNextFreeHotkey();

	void setName(const char * _name);
	const char * getName();
	// void setType(const char * _type);
	const char * getType();
	void setTemplateName(const char * _templateName);
	const char * getTemplateName();

	bool addOrReplaceChild(AALItem * _child);
	bool eraseChild(AALItem * _child);

	void setParent(AALItem * _parent);
	AALItem * getParent();

	virtual void toDOMElement(DOMDocument * _doc, DOMElement * _parentNode);
	virtual void fromDOMElement(DOMElement * _selfNode, int _currentDepth = 0);
	virtual void fromSVG(DOMDocument * _doc, DOMElement * _selfSvgNode, const char * _svgFile, const map<string, AALArea> & _svgAreas);

	virtual void toDOMElementParameters(DOMElement * _selfNode);
	virtual void fromDOMElementParameters(DOMElement * _selfNode, const char * _namespace = "");

	virtual void resize(AALSize * _size, bool _recursive);

	void setVisibility(DOMElement * _selfSvgNode, const bool _visible);
	void updateArea(const char * _subId, const map<string, AALArea> & _svgAreas);

	AALItem * getFirstChild();

	/**
	 * Depth-Firs Search.
	 * self, self.children, children.children and so on until a match.
	 * Returns AALItem * or NULL if not found.
	 */
	AALItem * getAALItem(const int _aalItemId);

	/**
	 * Depth-Firs Search.
	 * self, self.children, children.children and so on until a match.
	 * Returns AALItem * or NULL if not found.
	 */
	AALItem * getAALItemByHotkey(const int _aalItemHotkey);

	void renderImage(DrawPane * _target, AALLocation * _location, const int _renderDepth = 100, int _currentDepth = 0);
	virtual void renderHotkeys(DrawPane * _target, const int _renderDepth = 100, int _currentDepth = 0);

	virtual void visualizeSelf(Visualizer * _visualizer, AALAction * _action);

	virtual eOSState guiAction(eKeys _key);

	virtual void getFlatList(list<AALItem *> * _list, int _currentLevel);

	bool cloneRelatives(AALItem * _intoAALitem);
};
#endif /* AALITEM_H_ */
